﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TdolDemo_QBZ.Models;
using TdolDemo_QBZ.Models.Task;

namespace TdolDemo_QBZ.Repository
{
    public class TdolUnitRepository:ITdolUnitRepository
    {
        //get list
        public List<UnitModel> GetUnitInTask(List<int> taskid)
        {
            var units = new List<UnitModel>();
            var db = new sqltestContext();
            units = db.TbTdolTaskUnits
                    .Where(s => taskid.Contains(s.TaskId))
                    .Select(s => new UnitModel {
                        CreateBy =  s.CreateBy,
                        CreateOn = s.CreateOn,
                        Guid = s.Guid,
                        TaskId = s.TaskId,
                        TimeStamp = s.TimeStamp,
                        UnitId = s.UnitId,
                        UnitType = s.UnitType,
                        UpdateBy = s.UpdateBy,
                        UpdateOn =s.UpdateOn,
                        WorkUnitTime = s.WorkUnitTime
                     })
                    .ToList();
            return units;
        }

        public List<UnitModel> GetUnitInTask(int taskid)
        {
            return GetUnitInTask(new List<int> { taskid });
        }
        
        //add
        public int AddUnit(UnitModel unit)
        {
            return AddUnit(new List<UnitModel> { unit });
        }

        public int AddUnit(List<UnitModel> units)
        {
            var db = new sqltestContext();
            foreach(var unit in units)
            {
                var tb = new TbTdolTaskUnits
                {
                    CreateBy = unit.CreateBy,
                    CreateOn = unit.CreateOn,
                    Guid = unit.Guid,
                    TaskId = unit.TaskId,
                    UnitType = unit.UnitType,
                    WorkUnitTime = unit.WorkUnitTime
                };
                db.TbTdolTaskUnits.Add(tb);
            }
            return db.SaveChanges();
        }
    }
}
